﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Func : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("两数之和：" + MyMath.Sum(1,2) + "<br>");
        Response.Write("两数之和：" + MyMath.Sum(1, 2,3,4,5) + "<br>");
        Response.Write("两数之和：" + MyMath.Sum() + "<br>");

        int[] number = { 1,23,3,5,-5,100,6,12,-8};
        int? maxIndex = null;   //ref必须有初始值
        int? max = MyMath.Max(number,ref maxIndex);
        Response.Write("最大值：" + max + ",索引为" + maxIndex +"<br>");

        int? minIndex;   //out 可以没有初始值
        int? min = MyMath.Min(number, out minIndex);
        Response.Write("最大值：" + min + ",索引为" + minIndex + "<br>");
    }
}

public class MyMath {
    private static object result;

    /// <summary>
    /// 求两个整数之和
    /// </summary>
    /// <param name="a">操作数1</param>
    /// <param name="b">操作数2</param>
    /// <returns>两数之和</returns>
    public static int Sum(int a,int b) {
        return a + b;
    }

    /// <summary>
    /// 求多个数的和
    /// </summary>
    /// <param name="values">操作数集合（0个或多个）</param>
    /// <returns>多数之和（可为空）</returns>
    public static int? Sum(params int[] values) {
        int? result = null;

        if (values.Length>0) {
            result = 0;
            foreach (var value in values) {
                result += value;
            }
        }

        return result;
    }

    /// <summary>
    /// 求一组数中的最大值
    /// </summary>
    /// <param name="values">数据结合</param>
    /// <param name="maxIndex">返回最大值的索引值</param>
    /// <returns>最大值</returns>
    public static int? Max(int[] values,ref int? maxIndex) {
        if (values.Length <= 0)
        {
            maxIndex = null;
            return null;
        }
        else {
            var max = values[0];
            maxIndex = 0;
            for (int i=0;i<values.Length;i++) {
                if (values[i]>max) {
                    max = values[i];
                    maxIndex = i;
                }
            }
            return max;
        }
    }

    /// <summary>
    /// 求一组数中的最小值
    /// </summary>
    /// <param name="values">数据集合</param>
    /// <param name="minIndex">返回最小值的索引值</param>
    /// <returns>最小值</returns>
    public static int? Min(int[] values,out int? minIndex) {
        if (values.Length <= 0)
        {
            minIndex = null;
            return null;
        }
        else {
            var min = values[0];
            minIndex = 0;
            for (int i=0;i<values.Length;i++) {
                if (values[i]<min) {
                    min = values[i];
                    minIndex = i;
                }
            }
            return min;
        }
    }
}